package org.jeecg.modules.demo.com.kunpeng.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.com.kunpeng.entity.CommodityBrand;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.CommodityBrandQueryListDTO;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceCode;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceException;
import org.jeecg.modules.demo.com.kunpeng.mapper.CommodityBrandMapper;
import org.jeecg.modules.demo.com.kunpeng.service.ICommodityBrandService;
import org.jeecg.modules.demo.com.kunpeng.vo.CommodityBrandQueryListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 商品品牌
 * @Author: jeecg-boot
 * @Date:   2023-01-12
 * @Version: V1.0
 */
@Service
public class CommodityBrandServiceImpl extends ServiceImpl<CommodityBrandMapper, CommodityBrand> implements ICommodityBrandService {

    @Autowired
    private CommodityBrandMapper commodityBrandMapper;

    /**
     * 新增
     * @param commodityBrand
     */
    @Override
    public void insert(CommodityBrand commodityBrand) {
        List<CommodityBrand> commodityBrands = commodityBrandMapper.queryListByBrandName(commodityBrand.getBrandName());
        if(commodityBrands.size()>0){
            String message="该商品品牌已存在，请重新填写！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        commodityBrand.setState("0");
        commodityBrandMapper.insert(commodityBrand);
    }

    /**
     * 根据品牌名称模糊查询
     * @param page
     * @param commodityBrandQueryListDTO
     * @return
     */
    @Override
    public Page<CommodityBrandQueryListVO> queryListByBrand(Page<CommodityBrandQueryListVO> page, CommodityBrandQueryListDTO commodityBrandQueryListDTO) {
        Page<CommodityBrandQueryListVO> commodityBrandQueryListVOPage = page.setRecords(commodityBrandMapper.queryListByBrand(page, commodityBrandQueryListDTO.getBrandKeyWords()));
        return commodityBrandQueryListVOPage;
    }

    /**
     * 根据品牌名称模糊查询全部
     * @param page
     * @param commodityBrandQueryListDTO
     * @return
     */
    @Override
    public Page<CommodityBrandQueryListVO> queryListByBrandAll(Page<CommodityBrandQueryListVO> page, CommodityBrandQueryListDTO commodityBrandQueryListDTO) {
        Page<CommodityBrandQueryListVO> commodityBrandQueryListVOPage = page.setRecords(commodityBrandMapper.queryListByBrandAll(page, commodityBrandQueryListDTO.getBrandKeyWords()));
        return commodityBrandQueryListVOPage;
    }


    /**
     * 启用商品品牌
     */
    @Override
    public void enableBrand(String id) {
        int num = commodityBrandMapper.enableBrand(id);
        if(num!=1){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"启用失败，请联系管理员！");
        }
    }


    /**
     * 禁用商品品牌
     */
    @Override
    public void unEnableBrand(String id) {
        int num = commodityBrandMapper.unEnableBrand(id);
        if(num!=1){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"禁用失败，请联系管理员！");
        }
    }
}
